package com.dmjj.cadcommon.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王丽凯
 * @Description: TODO
 * @Title: PaginationVO
 * @ProjectName dmjj-parent
 * @date 2018/12/24
 */
@Data
@EqualsAndHashCode(callSuper=false)
@ApiModel("数据信息vo")
public class PaginationVO {

    @ApiModelProperty("返回的对象集")
    private List data;
    @ApiModelProperty("返回的对象")
    private Object object;
    @ApiModelProperty("返回当前页数")
    private Integer pageNum;
    @ApiModelProperty("返回总页数")
    private Integer total;

    public <T> PaginationVO(List<T> data, Class<?> object, Integer pageNum){
        List reslut = new ArrayList();
        for (int i=0; i<data.size(); i++){
            if (data.get(i) instanceof Integer){
                this.total= (Integer) data.get(i);
                data.remove(i);
            }else {
                try {
                    Object entity =  object.newInstance();
                    BeanUtils.copyProperties(data.get(i),entity);
                    reslut.add(entity);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        this.data = reslut;
        this.object = object;
        this.pageNum = pageNum;
    }
}
